const router = require('../../../utils/router.js')
const api = require('../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shopingCart: {},
    goodsID: null,
    goodsDetail: null,
    imageHost: api.IMG_HOST,
    selectGoodsAttrsModel: {
      visible: false,
      type: 'select'
    },
    currentGoods: null,
    buyCount: 1,
    goodsList: [],
    skuList: []
  },
  previewImage(e) {
    var list = []
    this.data.swiperList.map(item => {
      list.push(item.url)
    })
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: list
    })
  },
  showSelectGoodsAttrsClick(e) {
    this.setData({
      selectGoodsAttrsModel: {
        visible: true,
        type: e.currentTarget.dataset.type
      }
    })
  },
  closeSelectGoodsAttrsClick() {
    this.setData({
      selectGoodsAttrsModel: {
        visible: false
      }
    })
  },
  tapNavigateToUrl({ currentTarget }) {
    let url = currentTarget.dataset.url
    router.push(url)
  },
  skuChange ({detail}) {
    this.setData({
      currentGoods: detail.currentGoods,
      buyCount: detail.count
    })
  },
  /**
   * 添加商品到购物清单
   */
  addToShoppingCart() {
    if (!this.data.shopingCart || !this.data.shopingCart.id) {
      wx.showToast({
        title: '没有绑定购物清单',
        icon: 'none'
      })
      return false
    }
    return new Promise(resolve => {
      api._post('/User/Design/Goods/Add', {
        goodsSkuInfoID: this.data.currentGoods.Goods_Sku_InfoID,
        number: this.data.buyCount,
        designInfoID: this.data.shopingCart.id
      }).then(res => {
        if (res.Code === 100000) {
          this.saveShopingCartToStorage(this.data.buyCount)
          this.closeSelectGoodsAttrsClick()
          this.setData({
            buyCount: 1
          })
        }
      })
    })
  },
  onPageScroll(option) {
    this.setData({
      scrollTop: option.scrollTop
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({ goodsID }) {
    this.setData({
      goodsID: goodsID
    })
    this.getGoodsDetail().then(data => {
      this.setData({
        goodsDetail: data,
        goodsList: data.SkuInfoDetailsList,
        skuList: data.SkuGroupItemDetailsList
      })
      if (this.data.goodsList && this.data.goodsList.length > 0) {
        this.setData({
          currentGoods: this.data.goodsList[0]
        })
      }
    })
  },
  getGoodsDetail() {
    return new Promise(resolve => {
      api._get('/Mall/Goods/Details', {
        realGoodsID: this.data.goodsID
      }).then(res => {
        if (res.Code === 100000) {
          resolve(res.Data)
        }
      })
    })
  },
  /**
   * 从Storage中获取购物车信息
   */
  getShopCartFormStorage () {
    const shopingCart = wx.getStorageSync('shopingCart')
    this.setData({
      shopingCart: shopingCart
    })
  },
  saveShopingCartToStorage(count) {
    let shopingCart = this.data.shopingCart
    shopingCart.totalcount = shopingCart.totalcount + count
    wx.setStorageSync('shopingCart', shopingCart)
    this.setData({
      shopingCart: shopingCart
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getShopCartFormStorage()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})